<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片式显示界面 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="addCateDialog"
          >添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 商品分类界面 -->
      <el-table
        :data="cateTableInfo"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        row-key="cat_id"
        border
        stripe
      >
        <el-table-column
          type="index"
          label="序号"
        ></el-table-column>
        <el-table-column
          label="分类名称"
          prop="cat_name"
        ></el-table-column>
        <el-table-column label="是否有效">
          <template slot-scope="scope">
            <el-tag v-show="!scope.row.cat_deleted">有效</el-tag>
            <el-tag
              v-show="scope.row.cat_deleted"
              type="danger"
            >无效</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="排序"
          prop="cat_level"
        >
          <template slot-scope="scope">
            <el-tag v-show="scope.row.cat_level === 0">一级</el-tag>
            <el-tag
              type="success"
              v-show="scope.row.cat_level === 1"
            >二级</el-tag>
            <el-tag
              type="danger"
              v-show="scope.row.cat_level === 2"
            >三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="setCateTableInfo(scope.row)"
            >编辑</el-button>
            <el-button type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pageszie"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>

      <!-- 添加分类弹窗 -->
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="changeAddCateDialogVisibleClose"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="11%"
      >
        <el-form-item
          label="分类名称"
          prop="cat_name"
        >
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item
          label="父级分类"
          prop="cat_name"
        >
          <div class="block">
            <span class="demonstration"></span>
            <el-cascader
              v-model="testValue"
              :options="catParentOptions"
              @change='catParentChanged'
              :props="cscaderProps"
              clearable
            ></el-cascader>
          </div>
        </el-form-item>

      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCateTableInfo"
        >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑分类弹窗 -->
    <el-dialog
      title="编辑分类"
      :visible.sync="setCateDialogVisible"
      width="50%"
      @close="changeSetCateDialogVisibleClose"
    >
      <div>
        <span>编辑分类</span>
        <el-input v-model="setCateList.cat_name"></el-input>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="setCateDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="setCateInfo"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    // var checkCatName = (rule, value, cb) => {
    //   const regCatName = /^([a-zA-Z0-9_-]+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]))/
    //   if (regCatName.test(value)) {
    //     return cb()
    //   }
    //   cb(new Error('分类名称格式错误'))
    // }
    return {
      cateTableInfo: [],
      // 控制分页
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      },
      testValue: [],
      catParentOptions: [],
      cscaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      setCateDialogVisible: false, // 设置编辑弹窗
      setCateList: {} // 编辑弹窗获取数据存储
    }
  },
  created () {
    this.getCateTableInfo()
  },
  methods: {
    async getCateTableInfo () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取分类失败')
      this.$message.success('获取分类成功')
      // 数据列表赋值
      this.cateTableInfo = res.data.result
      this.total = res.data.total
    },
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getCateTableInfo()
    },
    handleCurrentChange (newcurrent) {
      this.queryInfo.pagenum = newcurrent
      this.getCateTableInfo()
    },
    // 点击编辑按钮编辑弹窗弹出获取分类ID
    async setCateTableInfo (id) {
      const { data: res } = await this.$http.get(`categories/${id.cat_id}`)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.$message.success('获取数据成功')
      this.setCateList = res.data
      this.setCateDialogVisible = true
    },
    // 点击添加分类按钮弹出添加分类弹窗
    addCateDialog () {
      this.addCateDialogVisible = true
      this.getCateParentList()
    },
    // 点击确定按钮确认添加分类 并且清空本地数据
    async addCateTableInfo () {
      // console.log(this.addCateForm)
      const { data: res } = await this.$http.post('categories', this.addCateForm)
      console.log(res)
      if (res.meta.status !== 201) return this.$message.error('创建失败')
      this.$message.success('创建成功')
      this.changeAddCateDialogVisibleClose()
      this.getCateTableInfo()
      this.addCateDialogVisible = false
    },
    // 添加分类弹窗关闭时清空本地数据
    changeAddCateDialogVisibleClose () {
      this.addCateForm.cat_name = ''
      this.catParentOptions = []
      this.cscaderProps.cat_level = 0
      this.cscaderProps.cat_pid = 0
      this.testValue = []
    },
    // 获取父级分类数据
    async getCateParentList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类失败')
      console.log(res)
      this.catParentOptions = res.data
    },
    // 多联选择器数据发生改变
    catParentChanged () {
      this.addCateForm.cat_level = this.testValue.length - 1
      if (this.testValue.length > 1) {
        this.addCateForm.cat_pid = this.testValue[this.testValue.length - 1]
      } else { this.addCateForm.cat_pid = this.testValue[0] }
    },
    // 点击确认按钮提交编辑完的数据
    setCateInfo () { },
    // 关闭编辑弹窗时清理本地数据
    changeSetCateDialogVisibleClose () { }
  }
}
</script>

<style scoped>
.el-breadcrumb {
  font-size: 12px;
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
.el-input {
  width: 90%;
  margin-left: 15px;
}
</style>
// 新版本在cascaderProps里配置checkStrictly:true,
